package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.adapter.HolderAdapter.ViewCreator;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 开发
 *
 * @author macbook
 */
public class DevelopmentAct extends BaseActivity implements OnItemClickListener, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    private HolderAdapter<HashMap<String, String>> mAdapter;
    private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private ListView lv_development;
    private Context context = DevelopmentAct.this;
    private SwipeRefreshLayout mSwipeLayout;
    private String imgUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_enquiry);
        initBar();
        actionbar_side_tab_name.setVisibility(View.VISIBLE);
        ll_iv_actionbar_tab_side_left.setVisibility(View.VISIBLE);
        actionbar_side_tab_name.setText("我的制版打样单");
        tv_actionbar_tab_side_right.setVisibility(View.VISIBLE);
        tv_actionbar_tab_side_right.setText("新建");
        tabBack();
        initView();
    }

    private void initView() {
        lv_development = (ListView) findViewById(R.id.lv_enquiry_list);
        lv_development.setOnItemClickListener(this);
        tv_actionbar_tab_side_right.setOnClickListener(this);
        createAdapter();
        loadingDevelopment();
        mAdapter.update(data);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.rl_modulename_refresh);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
    }

    private void loadingDevelopment() {
        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                JSONObject jsonObject = Tool
                        .getJsonObject(result);
                JSONArray array;
                if (Tool.getString(jsonObject, "resultCode").equals("0")) {
                    array = Tool.getJsonArray(jsonObject, "needList");
                    data = Tool.getListMapByJsonArray(array);
                    mAdapter.update(data);
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostNeedList(context, ioAuthCallBack);
    }

    public void createAdapter() {
        ViewCreator<HashMap<String, String>> creator = new ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {
                return LayoutInflater.from(context).inflate(R.layout.items_listview_enquiry, null);
            }

            @Override
            public void updateView(View view, int position, HashMap<String, String> map) {
                ImageView iv_enquiry_pic = (ImageView) view
                        .findViewById(R.id.iv_enquiry_pic);
                TextView tv_enquiry_productname = (TextView) view
                        .findViewById(R.id.tv_enquiry_productname);
                TextView tv_enquiry_number = (TextView) view
                        .findViewById(R.id.tv_enquiry_enquiryId);
                TextView tv_enquiry_statusnote = (TextView) view.findViewById(R.id.tv_enquiry_statusnote);
                tv_enquiry_statusnote.setText(map.get("statusNote"));
                tv_enquiry_productname.setText(map.get("developProductName"));
                tv_enquiry_number.setText("单号:" + map.get("needId"));
                imgUrl = data.get(position).get("mainImage");
                if (!TextUtils.isEmpty(imgUrl)) {
                    if (!imgUrl.substring(0, 1).equals("h") && !imgUrl.substring(0, 1).equals("H")) {
                        imgUrl = "http://" + imgUrl;
                    }
                }
                Glide.with(context)
                        .load(imgUrl)
                        .centerCrop()
                        .placeholder(R.drawable.empty_photo)
                        .crossFade()
                        .into(iv_enquiry_pic);
            }
        };
        mAdapter = new HolderAdapter<>(this.getLayoutInflater(), creator);
        lv_development.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(context, DevelopmentDetailAct.class);
        intent.putExtra("needId", data.get(position).get("needId"));
        intent.putExtra("imgUrl", imgUrl);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_actionbar_tab_side_right:
                Intent intent = new Intent(context, NewDevelopmenAct.class);
                startActivity(intent);
                break;
        }
    }


    protected void onResume() {
        loadingDevelopment();
        super.onResume();
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);
                loadingDevelopment();
            }
        }, 2000);
    }
}
